package cn.hxzy.service.impl;

import cn.hxzy.common.R;
import cn.hxzy.entity.Train;
import cn.hxzy.mapper.TrainMapper;
import cn.hxzy.service.TrainService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankan.WANG
 * @since 2022-01-13
 */
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements TrainService {
    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    public R sel(){
        QueryWrapper wrapper=new QueryWrapper();
        List<Train> r = trainMapper.selectList(wrapper);
        for (Train train:r) {
            redisTemplate.opsForList().leftPush("train",train);
        }
        return R.ok().message("success").data("train",r);
    }

//    @Override
//    public Boolean del(Integer trainId) {
//        Train train = trainMapper.selectById(trainId);
//        if (!Objects.isNull(train)){
//            int deleteById = trainMapper.deleteById(trainId);
//            if (deleteById>0){
//                redisTemplate.delete("train");
//                redisTemplate.opsForList().leftPushAll("train",train);
//                return true;
//            }
//        }
//        return false;
//    }

    public R update1(Train train) {
//        trainMapper.selectList(train);
        List<Train> train1 = redisTemplate.opsForList().range("train", 0, -1);
              redisTemplate.opsForList().remove("train",0,-1);
        int i = trainMapper.updateById(train);
        if (i>0) {
            QueryWrapper wrapper=new QueryWrapper();
            List<Train> list = trainMapper.selectList(wrapper);
            redisTemplate.opsForList().leftPushAll("train", list);
            return R.ok().data("item",i).message("修改成功");
        }
        return R.error().message("修改失败");}


        @Override
        public R upd1(Integer trainId){
            Train train = trainMapper.selectById(trainId);
            if ((!Objects.isNull(train))&&train.getStatus()!=1){
                Train train1 = train.setStatus(1);
                int update = trainMapper.updateById(train1);
                if (update>0){
                    redisTemplate.delete("train");
                    QueryWrapper wrapper=new QueryWrapper();
                    List<Train> list = trainMapper.selectList(wrapper);
                    redisTemplate.opsForList().leftPushAll("train",list);
                    return R.ok().data("item",train).message("修改成功");
                }
            }
            return R.error().code(-20000).message("修改失败");
    }

    @Override
    public R upd2(Integer trainId){
        Train train = trainMapper.selectById(trainId);
        if ((!Objects.isNull(train))&&train.getStatus()!=2){
            Train train1 = train.setStatus(2);
            int update = trainMapper.updateById(train1);
            if (update>0){
                redisTemplate.delete("train");
                QueryWrapper wrapper=new QueryWrapper();
                List<Train> list = trainMapper.selectList(wrapper);
                redisTemplate.opsForList().leftPushAll("train",list);
                return R.ok().data("item",train).message("修改成功");
            }
        }
        return R.error().code(-20000).message("修改失败");
    }

    @Override
    public R add(Train train) {
        if (redisTemplate.hasKey("train")){
            List<Train> t1 = redisTemplate.opsForList().range("train", 0, -1);
            for (Train train1:t1){
                if (train.getTrainName()!=train1.getTrainName()){
                    redisTemplate.opsForList().leftPush("train",t1);
                    System.out.println("添加成功");
                }else {
                    System.out.println("添加失败");
                }
            }
            System.out.println(train.toString());
            int insert = trainMapper.insert(train);
            if (insert>0){
                return R.ok().code(20000).message("添加成功").data("train",insert);
            }else {
                return R.error().code(20000).message("添加失败");
            }
        }
        redisTemplate.opsForList().leftPush("train",train);
        int insert = trainMapper.insert(train);
        if (insert>0){
            return R.ok().code(20000).message("添加成功").data("item",insert);
        }else {
            return R.error().code(20000).message("添加失败");
        }
    }

}
